package server.business.service.Impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.google.gson.Gson;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import server.business.mapper.AllBusinessMapper;
import server.business.mapper.BusinessmsgMapper;
import server.business.mapper.ProcessEntityMapper;
import server.business.pojo.Allbusiness;
import server.business.pojo.Businessmsg;
import server.business.pojo.Front;

import server.business.pojo.ProcessEntity;
import server.business.service.BusinessMsgsercice;
import server.business.service.ProcessService;
import server.business.utils.JsonUtils;
import server.business.utils.Otherutils;

import java.util.ArrayList;
import java.util.List;

@Service("BusinessMsgservice")
public class BusinessMsgserviceImpl extends ServiceImpl<BusinessmsgMapper, Businessmsg> implements BusinessMsgsercice {
    @Autowired
    BusinessmsgMapper businessmsgMap;


    @Autowired
    AllBusinessMapper allbusinessMap;

    @Autowired
    ProcessEntityMapper processMap;

    @Autowired
    ProcessService processService;
    @Override
    public Businessmsg getcurrentById(Integer id) {
        return getById(id);
    }

    @Override
    public List<Businessmsg> findall(String doctorid) {
        List<Businessmsg> businessMsg = businessmsgMap.getBusinessMsg(doctorid);

        return businessMsg;
    }

    @Override
    public String getcunrrentbusinessmsg(String doctorid) {
        List<Businessmsg> businessMsg = businessmsgMap.getBusinessMsg(doctorid);
        JSONObject goalmsg = new JSONObject();
//        Process process = new Process();
        List<String> processes = new ArrayList<>();
        for (Businessmsg businessmsg : businessMsg) {
            ProcessEntity process = processMap.selectById(Integer.parseInt(businessmsg.getCurrentprocess()));
//            System.out.println(process);
            Allbusiness allbusiness = allbusinessMap.selectById(businessmsg.getBussinessid());
            JSONObject temp = new JSONObject();
            try {
                temp.put("businessid",businessmsg.getBussinessid());
                temp.put("businessname",allbusiness.getBussnessname());
                temp.put("fronturl",process.getFrontpath());
                processes.add(temp.toString());

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        try {
            goalmsg.put("businessmsgs",processes);
            System.out.println(goalmsg);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        System.out.println(processes);
        return  goalmsg.toString();
    }

    @Override
    public List<Front> getelement(String doctorid) {
        Gson gson = new Gson();
        List<Businessmsg> businessMsg = businessmsgMap.getBusinessMsg(doctorid);
        JSONObject goalmsg = new JSONObject();
//        Process process = new Process();
        List<Front> processes = new ArrayList<>();
        for (Businessmsg businessmsg : businessMsg) {
            ProcessEntity process = processMap.selectById(Integer.parseInt(businessmsg.getCurrentprocess()));
//            System.out.println(process);
            Allbusiness allbusiness = allbusinessMap.selectById(businessmsg.getBussinessid());
            Front front = new Front();

            try {
                front.setId(businessmsg.getBussinessid());
                front.setBussnessname(allbusiness.getBussnessname());
                front.setFrontpath(process.getFrontpath());
//                temp.put("businessid",businessmsg.getBussinessid());
//                temp.put("businessname",allbusiness.getBussnessname());
//                temp.put("fronturl",process.getFrontpath());
//                JSONObject temp = new JSONObject();
//                String personjson = gson.toJson(front);
                processes.add(front);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
//        try {
//            goalmsg.put("businessmsgs",processes);
//            System.out.println(goalmsg);
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
        System.out.println(processes);
        return  processes;
    }

    @Override
    public boolean updateProcess(Integer id, String currentprocess, String currentprocess1,String currentposition,String msg,String operatehistory) {
        businessmsgMap.updateProcess(id,currentprocess,currentprocess1,currentposition,msg,operatehistory);

        return true;
    }

    @Override
    public IPage<Front> selectListInfoByPage(Page<Front> page, String doctorid) {
        return businessmsgMap.selectListInfoByPage(page,doctorid);
    }

    @Override
    public String getallmsg(String process) {
        String[] processes = Otherutils.getString(process);
        List<String> goal = new ArrayList<>();
        for (String proc : processes) {
            ProcessEntity processByid = processService.getProcessByid(Integer.parseInt(proc));
            //
            String[] inputmsg = Otherutils.getString(processByid.getInputmsg());
            for (String s : inputmsg) {
                if(!s.equals("null")){
                    if(!goal.contains(s)){
                        goal.add(s);
                    }
                }

            }
            String[] ouputmsg = Otherutils.getString(processByid.getOutputmsg());
            for (String s : ouputmsg) {
                if(!s.equals("null")){
                    if(!goal.contains(s)){
                        goal.add(s);
                    }
                }
            }
        }
        int size = goal.size();
        String[] goalstr = (String[])goal.toArray(new String[size]);
        return JsonUtils.createjsonstr(goalstr);
    }

    @Override
    public IPage<Businessmsg> selectList(Page<Businessmsg> page) {


        return businessmsgMap.selectList(page);
    }


    public List<Businessmsg> getbus(String id){
//        List<Businessmsg> datas = bas

        return null;
    }
}
